/**
 * ===========================================================================
 *
 *       Filename:  protocal.h
 *
 *    Description:  learn more
 *
 *        Version:  1.0
 *        Created:  04/14/09 22:42:33
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Author (mn), Email@xxx.xxx
 *        Company:  xxx xxx
 *
 * ===========================================================================
 */

#ifndef __BASE_PROTO_H__
#define __BASE_PROTO_H__

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <ctype.h>
#include <err.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <sys/resource.h>

#include <event.h>

#include "base_type.h"

//////////////////////////////////////////////////////////////////////

#pragma pack(1)

// 服务器基本信息.
struct server_info_t {
	int16_t id;
	char ip[DEF_IP_LEN];
	int16_t port;
	int8_t type;
	int8_t press;
};

// 用户基本信息.
struct user_info_t {
	int64_t id;
	char name[DEF_STR_SHORT];
	char nick[DEF_STR_SHORT];
	int8_t sex;
	int8_t age;
	char birthday[DEF_STR_SMALL];
	char email[DEF_STR_SHORT];
	char passwd[DEF_STR_SHORT];
	char note[DEF_STR_LEN];
};

// 组基本信息.
struct group_info_t {
	int64_t id;
	char name[DEF_STR_SHORT];
	int8_t type;
	int64_t  creater_id;
	char note[DEF_STR_LEN];
};

// 用户连接信息.
struct connect_t
{
	int fd;			// 连接
	struct bufferevent *buf_event;

	int type;		// 服务器,客户端
	int flag;
	int stat;		// 连接, 超时, 断开.

	time_t start;
	time_t last;

	struct user_info_t user;

};

////////////////////////////////////////////////////////////////////////////////

// 协议头定义.
#define DEF_HEAD_LENGTH		25
struct proto_head_t
{
	int16_t magic;		// 协议标识.
	int16_t ver;			// 版本.
	int16_t flag;		// 特殊用途标识.
	int16_t type;		// 协议号.
	int16_t len;			// 消息体长度.
};

// 向管理中心服务器汇报压力.
#define MSG_PRESS_INFO			30001
struct press_info_t
{
	int16_t id;		// ID.
	int8_t type;		// 类型.
//	int8_t place;		// 位置.
//	int8_t press;		// 压力.
	int8_t cpu;
	int8_t mem;
	int8_t bound;
};

// 管理中心发送的服务器列表.
#define MSG_SERVER_LIST		30002
struct server_list_t
{
	int16_t id;
	int8_t count;
	struct server_info_t server_list[];
};

////////////////////////////////////////////////////////////////////////

// 用户注册请求.
#define MSG_USER_REGISTER_REQ			10001
struct user_register_req_t
{
	int32_t session;
	struct user_info_t user_info;
};

// 用户注册回答.
#define MSG_USER_REGISTER_ANSWER		10002
struct user_register_resp_t
{
	int32_t session;
	int64_t id;
	int16_t result;
};

// 用户注销请求.
#define MSG_USER_UNREGISTER_REQ			10003
struct user_unregister_req_t
{
	int64_t id;
};

// 用户注销回答.
#define MSG_USER_UNREGISTER_RESP		10004
struct user_unregister_resp_t
{
	int64_t id;
	int16_t result;
};

// 用户登录请求.
#define MSG_USER_LOGIN_REQ				10005
struct user_login_req_t
{
	int64_t id;
	char passwd[DEF_STR_SHORT];
};

// 用户登录回答.
#define MSG_USER_LOGIN_RESP			10006
struct user_login_resp_t
{
	int64_t id;
	struct user_info_t user_info;
	int16_t result;
};

// 用户退出请求.
#define MSG_USER_LOGOUT_REQ				10007
struct user_logout_req_t
{
	int64_t id;
};

// 用户退出回答.
#define MSG_USER_LOGOUT_RESP			10008
struct user_logout_resp_t
{
	int64_t id;
	int16_t result;
};

// 用户添加好友请求.
#define MSG_USER_ADD_REQ				10009
struct user_add_req_t
{
	int64_t uid;
	int64_t fid;
	char note[DEF_STR_LEN];
};

// 用户添加好友回答.
#define MSG_USER_ADD_RESP				10010
struct user_add_resp_t
{
	int64_t uid;
	int64_t fid;
	int16_t result;
};

// 踢出一用户请求.
#define MSG_USER_KICK_REQ				10011
struct user_kick_req_t
{
	int64_t uid;
	int64_t kid;
	char note[DEF_STR_LEN];
};

// 踢出一用户回答.
#define MSG_USER_KICK_RESP			10012
struct user_kick_resp_t
{
	int64_t uid;
	int64_t kid;
	int16_t result;
};

//// 申请将别人加为好友.
//#define MSG_USER_ADD_CONFIRM_REQ		10013
//struct user_add_confirm_req_t
//{
//	unsigned long uid;
//	unsigned long fid;
//	char note[SHOAT_STR_MAX];
//};
//
//// 同意别人将自己加为好友.
//#define MSG_USER_ADD_CONFIRM_RESP		10014
//struct user_add_confirm_resp_t
//{
//	unsigned long uid;
//	unsigned long fid;
//	short result;
//};

// 用户更新基本信息.
#define MSG_USER_UPGRADE_REQ		10015
struct user_upgrade_req_t
{
	int64_t id;

	struct user_info_t user_info;
};

// 用户更新信息回答.
#define MSG_USER_UPGRADE_RESP			10016
struct user_upgrade_resp_t
{
	int64_t id;
	int16_t result;
};

// 请求用户基本信息.
#define MSG_USER_INFO_REQ			10017
struct user_info_req_t
{
	int64_t id;
};

// 请求用户基本信息回应.
#define MSG_USER_INFO_RESP		10018
struct user_info_resp_t
{
	int64_t id;
	struct user_info_t user_info;
	int16_t result;
};

//----------------------------------------------------------------------

// 创建群组申请.
#define MSG_GROUP_CREATE_REQ			10051
struct group_create_req_t
{
	int64_t uid;
	struct group_info_t group_info;
};

// 创建群组回答
#define MSG_GROUP_CREATE_RESP			10052
struct group_create_resp_t
{
	int64_t uid;
	int64_t gid;
	int16_t result;
};

// 删除群组请求.
#define MSG_GROUP_DELETE_REQ			10053
struct group_delete_req_t
{
	int64_t uid;
	int64_t gid;
	char passwd[DEF_STR_SHORT];
};

// 删除群组回答.
#define MSG_GROUP_DELETE_RESP			10054
struct group_delete_resp_t
{
	int64_t uid;
	int64_t gid;
	int16_t result;
};

// 加入群组请求.
#define MSG_GROUP_JOIN_REQ				10055
struct group_join_req_t
{
	int64_t uid;
	int64_t gid;
	char note[DEF_STR_LEN];
};

// 加入群组回答
#define MSG_GROUP_JOIN_RESP			10056
struct group_join_resp_t
{
	int64_t uid;
	int64_t gid;
	int16_t result;
};

// 退出群组请求.
#define MSG_GROUP_QUIT_REQ				10057
struct group_quit_req_t
{
	int64_t uid;
	int64_t gid;
	char note[DEF_STR_LEN];
};

// 退出群组回答.
#define MSG_GROUP_QUIT_RESP			10058
struct group_quit_resp_t
{
	int64_t uid;
	int64_t gid;
	int16_t result;
};

// 更新群组信息请求.
#define MSG_GROUP_UPGRADE_REQ		10059
struct group_upgrade_req_t
{
	int64_t uid;
	struct group_info_t group_info;
};

// 群组信息更新回答.
#define MSG_GROUP_UPGRADE_RESP	10060
struct group_upgrade_resp_t
{
	int64_t uid;
	int64_t gid;
	int16_t result;
};

// 请求群组信息.
#define MSG_GROUP_INFO_REQ			10061
struct group_info_req_t
{
	int64_t uid;
	int64_t gid;
};

// 请求群组信息回应.
#define MSG_GROUP_INFO_RESP		10062
struct group_info_resp_t
{
	int64_t uid;
	struct group_info_t group_info;
	int16_t result;
};

//----------------------------------------------------------------------

// 用户信息发送
#define MSG_SEND_USER					10101
struct msg_send_user_t
{
	int64_t fid;
	char fnick[DEF_STR_SHORT];
	int64_t tid;
	char msg[DEF_STR_LARGE];
};

// 用户信息接收
#define MSG_RECEIVE_USER				10102
struct msg_receive_user_t
{
	int64_t fid;
	char fnick[DEF_STR_SHORT];
	int64_t tid;
	char sndtime[DEF_STR_SMALL];
	char msg[DEF_STR_LARGE];
};

// 群组信息发送
#define MSG_SEND_GROUP					10103
struct msg_send_group_t
{
	int64_t uid;
	int64_t gid;
	char msg[DEF_STR_LARGE];
};

// 群组信息接收
#define MSG_RECEIVE_GROUP				10104
struct msg_receive_group_t
{
	int64_t uid;
	int64_t gid;
	char sndtime[DEF_STR_SMALL];
	char msg[DEF_STR_LARGE];
};

// 用户好友列表请求.
#define MSG_USER_FRIENDS_REQ				10151
struct user_friends_req_t
{
	int64_t uid;
};

// 用户好友列表回答
#define MSG_USER_FRIENDS_RESP			10152
struct user_friends_resp_t
{
	int64_t uid;
	int8_t count;
	struct user_info_t list[DEF_ARRAY_MAX];
	int16_t result;
};

// 群组列表请求.
#define MSG_USER_GROUPS_REQ				10153
struct user_groups_req_t
{
	int64_t uid;
};

// 群组列表回答
#define MSG_USER_GROUPS_RESP			10154
struct user_groups_resp_t
{
	int64_t uid;
	int8_t count;
	struct group_info_t list[DEF_ARRAY_MAX];
	int16_t result;
};

// 群组用户列表清求.
#define MSG_GROUP_USERS_REQ			10155
struct group_users_req_t
{
	int64_t uid;
	int64_t gid;
};

// 群组用户列表回答.
#define MSG_GROUP_USERS_RESP		10156
struct group_users_resp_t
{
	int64_t uid;
	int64_t gid;
	int8_t count;
	struct user_info_t list[DEF_ARRAY_MAX];
	int16_t result;
};

//--------------------------------------------------------------------------

// 服务器认证
#define MSG_SERVER_REG_REQ		20001
struct server_reg_req_t
{
	int16_t id;
	char ip[DEF_IP_LEN];
	int16_t port;
	int8_t type;
};

// 服务器认证回答
#define MSG_SERVER_REG_RESP		20002
struct server_reg_resp_t
{
	int16_t id;
	int16_t result;
};

// 发送服务器压力信息.
#define MSG_DISPATCH_PRESS     20007
struct dispatch_press_t
{
	int16_t id;
	int8_t press;
};

// 获取代理服务器信息
#define MSG_AGENT_GET_REQ	20009
struct agent_get_req_t
{
	// NULL.
};

// 向客户返回代理服务器地址
#define MSG_AGENT_GET_RESP   20010
struct agent_get_resp_t
{
	int16_t id;
	char ip[DEF_IP_LEN];
	int16_t port;
	int16_t result;
};

#pragma pack()

#endif // __BASE_PROTO_H__

